package com.cn.sys.controller;

import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.apache.shiro.subject.Subject;
//
//import org.apache.shiro.authc.AuthenticationException;
//import org.apache.shiro.authc.IncorrectCredentialsException;
//import org.apache.shiro.authc.LockedAccountException;
//import org.apache.shiro.authc.UnknownAccountException;
//import org.apache.shiro.authc.UsernamePasswordToken;
//import org.apache.shiro.crypto.hash.Sha256Hash;
//import org.apache.shiro.subject.Subject;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.cn.utils.FormSubmitResult;
import com.cn.utils.PageUtils;
import com.cn.utils.ShiroUtils;
//import com.google.code.kaptcha.Constants;
//import com.google.code.kaptcha.Producer;
import com.cn.utils.interceptor.AuthorizationInterceptor;

/**
 * 登录相关
 */
@Controller
public class SysLoginController {
//	@Autowired
//	private Producer producer;
	
   private org.slf4j.Logger logger= LoggerFactory.getLogger(getClass());
	
//	@RequestMapping("captcha.jpg")
//	public void captcha(HttpServletResponse response)throws ServletException, IOException {
//        response.setHeader("Cache-Control", "no-store, no-cache");
//        response.setContentType("image/jpeg");
//
//        //生成文字验证码
//        String text = producer.createText();
//        //生成图片验证码
//        BufferedImage image = producer.createImage(text);
//        //保存到shiro session
//        ShiroUtils.setSessionAttribute(Constants.KAPTCHA_SESSION_KEY, text);
//        
//        ServletOutputStream out = response.getOutputStream();
//        ImageIO.write(image, "jpg", out);
//	}
	
	/**
	 * 登录
	 */
	@ResponseBody
	@RequestMapping(value = "/sys/login", method = RequestMethod.POST)
	public FormSubmitResult login(String username, String password, String captcha,HttpServletRequest request)throws IOException {
//		String kaptcha = ShiroUtils.getKaptcha(Constants.KAPTCHA_SESSION_KEY);
//		if(!captcha.equalsIgnoreCase(kaptcha)){
//			return FormSubmitResult.error("验证码不正确");
//		}
		
		try{
			Subject subject = ShiroUtils.getSubject();
			//sha256加密
			password = new Sha256Hash(password).toHex();
			UsernamePasswordToken tSUCCESSen = new UsernamePasswordToken(username, password);
			subject.login(tSUCCESSen);
		}catch (UnknownAccountException e) {
			return FormSubmitResult.error(e.getMessage());
		}catch (IncorrectCredentialsException e) {
			return FormSubmitResult.error(e.getMessage());
		}catch (LockedAccountException e) {
			return FormSubmitResult.error(e.getMessage());
		}catch (AuthenticationException e) {
			return FormSubmitResult.error("账户验证失败");
		}
		
//		password = new Sha256Hash(password).toHex();
//		request.getSession().setAttribute(AuthorizationInterceptor.LOGIN_USER_KEY, username);
		logger.debug("login success");
		return FormSubmitResult.SUCCESS();
	}
	
	/**
	 * 退出
	 */
	@RequestMapping(value = "logout", method = RequestMethod.GET)
	public String logout() {
//		ShiroUtils.logout();
		return "redirect:login.html";
	}
	
	
	@RequestMapping(value = "/login",method = RequestMethod.GET)
    public String showLogin(HttpServletRequest request, HttpServletResponse response,Model model) {
		
        return "login";
    }

	
}
